package a1_2;

/**
 * 课程类（组合部分）：不可独立存在，必须依赖 College
 */
public class Course {
    // 课程基础属性
    private final String courseId;   // final：创建后不可修改
    private final String courseName;
    private final Building location;

    // 核心：持有整体（College）的强引用，final 保证绑定后不修改
    private final College college;

    // 1. 私有构造器：禁止外部直接创建 Course 实例
    private Course(String courseId, String courseName, Building location, College college) {
        this.courseId = courseId;
        this.courseName = courseName;
        this.location = location;
        this.college = college; // 强制绑定到某个 College，无 College 则无法创建
    }

    // 2. 仅提供 Getter，无 Setter：属性创建后不可修改，避免脱离 College
    public String getCourseId() {
        return courseId;
    }

    public String getCourseName() {
        return courseName;
    }

    public Building getLocation() {
        return location;
    }

    // 允许获取所属 College（体现部分-整体关联）
    public College getCollege() {
        return college;
    }

    // 3. 静态工厂方法（仅允许 College 调用，通过包访问权限或内部类实现）
    // 这里用包访问权限（无修饰符），确保只有同包的 College 能调用
    static Course createCourse(String courseId, String courseName, Building location, College college) {
        // 校验：College 不能为空，强制绑定
        if (college == null) {
            throw new IllegalArgumentException("Course must belong to a College");
        }
        return new Course(courseId, courseName, location, college);
    }
}
